var grid;
			var carpark;
			var mainTree;
			var carparkData;
			var fixCharge;
			var fixChargeData;
			var tempChargeData;
			$(function() {
				loadTree();
				loadGrid('0');
				carpark.checkUser($("#carparkManageTool"),"财务管理员")
			});

			function loadTree() {
				mainTree = $("#carparkTree").tree({
					url: '/carpark/listToLevel',
					method: 'post',
					animate: true,
					onClick: function(node) {
						if(node.attributes == null) {
							return;
						}
						carpark = node;
						loadGrid(node.attributes.id);
					}
				});
			}

			function loadGrid(cid) {
				grid = $('#grid').mygrid({
					title: '',
					url: '/carpark/listCharges?id=' + cid,
					pagination: false,
					frozenColumns: [
						[{
							width: '100',
							title: '编码',
							field: 'code',
							sortable: false
						}]
					],
					columns: [
						[{
							width: '100',
							title: '名称',
							field: 'name',
							sortable: false
						}, {
							width: '100',
							title: '收费类型',
							field: 'type',
							sortable: false
						}, {
							width: '150',
							title: '车辆类型',
							field: 'carType',
							sortable: false
						}, {
							width: '80',
							title: '节假日类型',
							field: 'holidayType',

						}, {
							width: '80',
							title: '是否启用',
							field: 'useType',

						}]
					],
					toolbar: '#toolbar',
					onClickRow: function(rowIndex, rowData) {
						fixCharge = rowData;
					}
				});
			}

			function addMainCarpark(title, data) {
				carparkData = data;
				var dialog = sy.modalDialog({
					title: title == null ? "添加停车场" : title,
					width: 400,
					height: 320,
					url: "addMainCarpark.html",
					buttons: [{
						text: '保存',
						handler: function() {
							dialog.find('iframe').get(0).contentWindow.submitNow(dialog, mainTree, $);
						}
					}, {
						text: '关闭',
						handler: function() {
							dialog.dialog("close")
						}
					}],
				});
			}

			function addChildCarpark(title, data, pid) {
				if(carpark == null && title == null) {
					return;
				}
				carparkData = data;
				var parentId = pid != null ? pid : carpark.attributes.id;
				var dialog = sy.modalDialog({
					title: title == null ? "添加子停车场" : title,
					width: 400,
					height: 260,
					url: "addChildCarpark.html",
					buttons: [{
						text: '保存',
						handler: function() {
							dialog.find('iframe').get(0).contentWindow.submitNow(parentId, dialog, mainTree, $);
						}
					}, {
						text: '关闭',
						handler: function() {
							dialog.dialog("close")
						}
					}],
				});
			}

			function editCarpark() {
				if(carpark == null) {
					return;
				}
				var data = carpark.attributes;
				if(data.parentId == null) {
					addMainCarpark("修改停车场", carpark);
				} else {
					addChildCarpark("修改子停车场", carpark, data.parentId);
				}

			}

			function deleteCarpark() {
				$.messager.confirm("确认", "确认删除停车场[" + carpark.text + "]吗？", function(r) {
					if(r) {
						var txt = "/carpark/deleteCarpark";
						$.post(txt, {
							id: carpark.attributes.id
						}, function(data) {
							if(!data.success) {
								$.messager.alert("提示", data.msg, "error");
							} else {
								$.messager.alert("提示", data.msg, "info");
								reloadTree();
							}
						});
					}
				});
			}

			function reloadTree() {
				mainTree.tree("reload");
				carpark = null;
			}

			function addTempCharge(title, data, pid) {
				if(carpark == null && title == null) {
					return;
				}
				tempChargeData = data;
				var parentId = pid != null ? pid : carpark.attributes.id;
				var dialog = sy.modalDialog({
					title: title == null ? "添加临时收费" : title,
					width: 800,
					height: 610,
					url: "addTempCharge.html",
					buttons: [{
						text: '保存',
						handler: function() {
							dialog.find('iframe').get(0).contentWindow.submitNow(parentId, dialog, mainTree, $);
						}
					}, {
						text: '关闭',
						handler: function() {
							dialog.dialog("close")
						}
					}],
				});
			}

			function addFixCharge(title, data, pid) {
				if(carpark == null && title == null) {
					return;
				}
				fixChargeData = data;
				var parentId = pid != null ? pid : carpark.attributes.id;
				var dialog = sy.modalDialog({
					title: title == null ? "添加固定收费" : title,
					width: 400,
					height: 360,
					url: "addFixCharge.html",
					buttons: [{
						text: '保存',
						handler: function() {
							dialog.find('iframe').get(0).contentWindow.submitNow(parentId, dialog, mainTree, $);
						}
					}, {
						text: '关闭',
						handler: function() {
							dialog.dialog("close")
						}
					}],
				});

			}

			function editCharge() {
				if(fixCharge == null) {
					return;
				}
				$.post("/carpark/getCharge", fixCharge, function(d) {
					if(d == null||d.id==null) {
						sy.info("收费不存在");
						return;
					}
					if(fixCharge.type == "临时收费") {
						tempChargeData=d;
						addTempCharge("修改临时收费",d,d.carparkId)
					} else {
						addFixCharge("修改固定收费", d, d.carparkId);
					}
				});
			}

			function deleteCharge() {
				var row = grid.datagrid('getSelected');
				if(row == null) {
					return;
				}
				$.messager.confirm("确认", "确认删除停车场收费[" + row.code + "-" + row.name + "]吗？", function(r) {
					if(r) {
						$.post("/carpark/deleteCharge", row, function(data) {
							sy.info(data.msg);
							if(data.success) {
								loadDataGrid();
							}
						});
					}
				});
			}

			function enableCharge(isUse) {
				var row = grid.datagrid('getSelected');
				if(carpark == null || row == null) {
					return;
				}
				if(row.type != "临时收费") {
					return;
				}
				if(isUse && row.useType == '已启用') {
					return;
				}
				if(!isUse && row.useType == '未启用') {
					return;
				}
				$.post("/carpark/enableCharge", {
					id: row.id,
					code: row.code,
					isUse: isUse
				}, function(data) {
					if(data.success) {
						loadGrid(carpark.attributes.id);
					} else {
						sy.info(data.msg);
					}
				});
			}
			
function loadDataGrid(){
	grid.datagrid("clearSelections");
	grid.datagrid("load",{});
}
